@extends('admin.master')

@section('css')
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <style>

        .file-input {
            margin: 10px 50px;
            position: relative;
            left: 46px;
        }

        .file-caption {
            margin-top: 5px;
            height: 36px;
        }
        .form-inline {
            margin-bottom: 20px;
        }

        .form-inline label {
            font-weight: bold;
            margin-right: 10px;
        }
    </style>
@stop

@section('page_header')
    <h1 class="page-title">
        <i class="voyager-person"></i>@if (empty($id)) 添加用户 @else 编辑用户 @endif
    </h1>
@stop

@section('content')
    <div class="page-content container-fluid">
        <div class="row">
            <div class="col-md-12">

                <div class="panel panel-bordered">

                    <div class="panel-heading">
                        <h3 class="panel-title">@if (empty($id)) 添加用户 @else 编辑用户 @endif
                            <small style="color:red">（*为必填项）</small>
                        </h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form role="form" class="form-horizontal"
                          action="{{ route('users.post_add') }}"
                          method="POST" enctype="multipart/form-data">
                        <!-- PUT Method if we are editing -->
                        <!-- CSRF TOKEN -->
                        {{ csrf_field() }}
                        <div class="panel-body">
                            <div class="form-inline col-sm-6">
                                <label>
                                    <small style="color:red">*</small>
                                    省份</label>
                                <select onchange="changeProvince();" name="province" id="province"
                                        class="form-control">
                                    <option value="null" selected="selected">--请选择省份--</option>
                                    @foreach($provinces as $province)
                                        <option @if(!empty($user) && $province['address']==$user->province)selected="selected"
                                                @endif value={{$province['address']}}>{{$province['address']}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="form-inline col-sm-6">
                                <label>
                                    <small style="color:red">*</small>
                                    城市</label>
                                <select onchange="getSuperiors();" name="city" id="city" class="form-control">
                                    <option value="null" selected="selected">--请选择城市--</option>
                                    @if($citys != null)
                                        @foreach($citys as $city)
                                            <option @if($city['id']==$user->city)selected="selected"
                                                    @endif value={{$city['id']}}>{{$city['city']}}</option>
                                        @endforeach
                                    @endif
                                </select>
                            </div>
                            <div class="form-inline col-sm-6">
                                <label for="name">
                                    <small style="color:red">*</small>
                                    姓名</label>
                                <input type="text" class="form-control" name="name"
                                       placeholder="姓名" id="name"
                                       value="@if (!empty($id)){{ $user->name }}@endif">
                                <input type="hidden" name="id" value="{{ $id }}">
                            </div>
                            <div class="form-inline col-sm-6">
                                <label for="name">
                                    <small style="color:red">*</small>
                                    邮箱</label>
                                <input type="text" class="form-control" name="email"
                                       placeholder="邮箱" id="email"
                                       value="@if (!empty($id)){{ $user->email }}@endif">
                            </div>
                            <div class="form-inline col-sm-6">
                                <label for="name">
                                    <small style="color:red">*</small>
                                    手机号</label>
                                <input type="text" class="form-control" name="mobile"
                                       placeholder="手机号" id="mobile"
                                       value="@if (!empty($id)){{ $user->mobile }}@endif">
                            </div>
                            <div class="form-inline col-sm-6">
                                <label for="password">@if (!isset($user->password))
                                        <small style="color:red">*</small>@endif密码</label>
                                <input type="password" class="form-control" name="password"
                                       placeholder="密码" id="password"
                                       value="">
                                @if (isset($user->password))
                                    <small style="color:red;">不改密码请留空</small>
                                @endif
                            </div>
                            <div class="form-inline col-sm-6">
                                <label><small style="color:red">*</small>用户角色</label>
                                <select onchange="getSuperiors();" name="role_id" id="role" class="form-control">
                                    @foreach ($roles as $role)
                                        @if (Auth::user()->role->id != 1 && $role->id == 1)
                                        @else
                                            <option @if(isset($user->role_id)) @if ($user->role_id == $role->id) selected
                                                    @endif @endif value="{{ $role->id }}">{{ $role->name }}</option>
                                        @endif
                                    @endforeach
                                </select>
                            </div>
                            <div class="form-inline col-sm-6">
                                <label><small style="color:red">*</small>上级员工</label>
                                <select name="superior" id="superior" class="form-control">
                                    <option value="null" selected="selected">--请选择上级员工--</option>
                                    @if(!empty($superiors))
                                        @foreach($superiors as $superior)
                                            <option @if(!empty($user) && $superior['id']==$user->superior)selected="selected"
                                                    @endif value={{$superior['id']}}>{{$superior['name']}}</option>
                                        @endforeach
                                    @endif
                                </select>
                            </div>
                            <div class="form-inline col-sm-6">
                                <label for="password">头像</label>
                                @if (isset($user->avatar))
                                    <img src="/storage/{{$user->avatar}}"
                                         style="width:200px; height:auto; clear:both; display:block; padding:2px; border:1px solid #ddd; margin-bottom:10px;">
                                @endif
                                <input type="file" name="avatar">
                            </div>
                        </div><!-- panel-body -->
                        <div class="panel-footer">
                            <button type="submit" onclick="return check();" class="btn btn-primary">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@stop
@section('javascript')
    <script>
        $('document').ready(function () {
            $('.toggleswitch').bootstrapToggle();
        });
    </script>
    <script>
        function changeProvince() {
            var url = "{{ route('admin.estate.get_city_by_province') }}";
            var province = $("#province").val();

            $("#district option").each(function () {
                if ($(this).val() != 'null') {
                    $(this).remove();
                }
            });

            $.ajax({
                url: url,
                data: {'province': province},
                type: "GET",
                dataType: "JSON",
                async: true,
                success: function (datas) {
                    $("#city option").each(function () {
                        if ($(this).val() != 'null') {
                            $(this).remove();
                        }
                    });
                    datas.forEach(function (data) {
                        str = "<option value='" + data.id + "'>" + data.city + "</option>";
                        $("#city").append(str);
                    });
                }
            });
        }
        function getSuperiors() {
            var url = "{{ route('admin.users.get_superiors') }}";
            var city_id = $("#city").val();
            var role_id = $("#role").val();
            var user_id = @if(!empty($user)) '{{$user['id']}}'
            @else''@endif;

            $("#superior option").each(function () {
                if ($(this).val() != 'null') {
                    $(this).remove();
                }
            });

            $.ajax({
                url: url,
                data: {'city_id': city_id, 'role_id': role_id, 'user_id': user_id},
                type: "POST",
                dataType: "JSON",
                async: true,
                success: function (datas) {
                    $("#superior option").each(function () {
                        if ($(this).val() != 'null') {
                            $(this).remove();
                        }
                    });
                    datas.forEach(function (data) {
                        str = "<option value='" + data.id + "'>" + data.name + "</option>";
                        $("#superior").append(str);
                    });
                }
            });
        }

        function check() {
            var name = $("#name").val();
            var email = $("#email").val();
            var mobile = $("#mobile").val();
            var role = $("#role").val();
            var superior = $("#superior").val();
            if (superior.trim() == '' && role != 1) {
                toastr.error("上级不能为空");
                return false;
            }
            if (name.trim() == '') {
                toastr.error("姓名不能为空");
                return false;
            }
            if (email.trim() == '') {
                toastr.error("邮箱不能为空");
                return false;
            }
            if (mobile.trim() == '') {
                toastr.error("手机号不能为空");
                return false;
            }
                    @if (!isset($user->password))
            var password = $("#password").val();
            if (password.trim() == '') {
                toastr.error("密码不能为空");
                return false;
            }
            @endif
        }
    </script>
    <script src="/js/tinymce.min.js"></script>
    <script src="/js/voyager_tinymce.js"></script>
@stop
